package controller;

import model.Customer;
import model.CustomerCollection;
import model.DuplicateCustomerIdentifierException;

/**
 *
 * @author Mads Nielsen
 *
 */
public class CustomerController {

    private CustomerCollection customers;

    public CustomerController() {
        customers = CustomerCollection.getInstance();
    }

    public static void loadData() throws DuplicateCustomerIdentifierException {

        Customer c1 = new Customer();
        c1.setName("Kunde 1");
        c1.setPhone(12345678);

        Customer c2 = new Customer();
        c2.setName("Kunde 2");
        c2.setPhone(23456789);

        Customer c3 = new Customer();
        c3.setName("Kunde 3");
        c3.setPhone(34567890);

        CustomerCollection customers = CustomerCollection.getInstance();
        customers.addCustomer(c1);
        customers.addCustomer(c2);
        customers.addCustomer(c3);

    }

    public void createCustomer(String name, int phone, String address, int zipCode, String city) throws DuplicateCustomerIdentifierException {
        Customer customer = new Customer();
        customer.setName(name);
        customer.setPhone(phone);
        customer.setAddress(address);
        customer.setZipCode(zipCode);
        customer.setCity(city);

        customers.addCustomer(customer);
    }

    public Customer[] getCustomers() {
        return customers.getCustomers();
    }

    public Customer getCustomer(int phone) {
        return customers.getCustomer(phone);
    }

    public void editCustomer(Customer customer, int newPhone, String newName,
            String newAddress, String newCity, int newZipCode) throws DuplicateCustomerIdentifierException {
        Customer lookUp = customers.getCustomer(newPhone);
        if (customer == lookUp || lookUp == null) {
            customer.setPhone(newPhone);
            customer.setName(newName);
            customer.setAddress(newAddress);
            customer.setCity(newCity);
            customer.setZipCode(newZipCode);
        } else {
            throw new DuplicateCustomerIdentifierException();
        }
    }

    public int getCustomerCount() {
        return customers.getCustomers().length;
    }
}
